package com.lqm.service.home.impl;

import com.lqm.dao.HomeUserDao;
import com.lqm.dao.ProjectSupportDao;
import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.Project;
import com.lqm.entity.home.ProjectSupport;
import com.lqm.service.home.ProjectSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

/**
 * @program: CrowdFunding
 * @description
 * @author: lqm
 * @create: 2018-12-16 19:02
 */
@Service
public class ProjectSupportServiceImpl implements ProjectSupportService {

    @Autowired
    ProjectSupportDao projectSupportDao;
    @Autowired
    HomeUserDao homeUserDao;
    @Override
    public double findSupportSumByProject(Project project) {
        Double supportSumByProject = projectSupportDao.findSupportSumByProject(project);
        if (supportSumByProject==null){
            return 0;
        }
        return supportSumByProject;
    }

    @Override
    public List<ProjectSupport> findAllByProject(int page, int limit,Project project) {
        Sort sort = new Sort(Sort.Direction.DESC, "supportId");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        return projectSupportDao.findAllByProject(project,pageable);
    }

    @Override
    public Integer findAllByProjectCount(Project project) {
        return projectSupportDao.findAllByProjectCount(project);
    }

    @Override
    public int findSupportSingleCount(Project project) {
        return projectSupportDao.findSupportSingleCount(project);
    }

    @Override
    public int findSupportUserCount(Project project) {
        Integer supportUserCount = projectSupportDao.findSupportUserCount(project);
        if (supportUserCount==null){
            return 0;
        }

        return projectSupportDao.findSupportUserCount(project);
    }

    @Override
    public List<ProjectSupport> findAllByHomeUser(int page, int limit,HomeUser homeUser) {

        Sort sort = new Sort(Sort.Direction.DESC, "supportId");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        return projectSupportDao.findAllByHomeUser(homeUser,pageable);
    }

    @Override
    public Integer findAllByHomeUserCount(HomeUser homeUser) {
        return projectSupportDao.findAllByHomeUserCount(homeUser);
    }


    @Override
    @Transactional
    public String support(ProjectSupport projectSupport,HomeUser homeUser) {
//        Double account = homeUser.getAccount();
        Integer num = projectSupport.getNum();
        BigDecimal priceB = projectSupport.getPrice();
        double price = Double.parseDouble(priceB.toString());
        Integer id = homeUser.getId();
        HomeUser byId = homeUserDao.getById(id);
        double all = num * price;
        if (byId.getAccount()>=all){
            homeUserDao.divMoney(byId,all);
            ProjectSupport save = projectSupportDao.save(projectSupport);
            return "投资成功";
        }


        return "余额不足" ;
    }

    /**
     * 发货
     * @param projectSupport
     * @return
     */
    @Override
    @Transactional
    public ProjectSupport sendSupport(ProjectSupport projectSupport) {
        ProjectSupport one = projectSupportDao.getOne(projectSupport.getSupportId());
        one.setProjectStatus(1);
        return  one;
    }
    /**
     * 完成此次回报
     * @param projectSupport
     * @return
     */
    @Override
    @Transactional
    public ProjectSupport finishSupport(ProjectSupport projectSupport) {
//        projectSupport.setProjectStatus(2);
        ProjectSupport one = projectSupportDao.getOne(projectSupport.getSupportId());
        one.setProjectStatus(2);
        return one;
    }
}
